#!/bin/env ruby
# encoding: utf-8
#**************************************************************************
shared_context "summary data" do   
  context "when submit Summary" do
    it "then it can not be empty"  do          	
      browser.refresh
      text_field(name: "summary",  id: "summary").clear
      button(text: "Submit").click      
      span(id: "summary.errors", text: "Please enter info here.").wait_until_present(5)
    end
 #   # it "then it can contain les than or equal to 65535 characters"  do      
    #   browser.refresh
    #   text_field(name: "summary",  id: "summary").clear
    #   text_field(name: "summary",  id: "summary").set str(33000)
    #   button(text: "Submit").click       
    #   span(id: "summary.errors", text: "Can contain less than or equal to 65535 characters").wait_until_present(5)      
    # end
  end  
  context "when submit experience" do
    it "then it can not be empty"  do   
      browser.refresh   
      text_field(name: "experience", id: "experience").clear
      button(text: "Submit").click      
      span(id: "experience.errors", text: "Please enter info here.").wait_until_present(5)
    end
#    # it "then it can contain les than or equal to 65535 characters"  do      
    #   browser.refresh
    #   text_field(name: "experience", id: "experience").clear
    #   text_field(name: "experience", id: "experience").set str(33000)
    #   button(text: "Submit").click       
    #   span(id: "experience.errors", text: "Can contain less than or equal to 65535 characters").wait_until_present(5)      
    # end
  end
  context "when submit  professional interests" do
    it "then it can not be empty"  do      
      browser.refresh
      text_field(name: "interests", id: "interests").clear
      button(text: "Submit").click      
      span(id: "interests.errors", text: "Please enter info here.").wait_until_present(5)
    end
  #  # it "then it can contain les than or equal to 65535 characters"  do      
    #   browser.refresh
    #   text_field(name: "interests", id: "interests").clear
    #   text_field(name: "interests", id: "interests").set str(33000)
    #   button(text: "Submit").click       
    #   span(id: "interests.errors", text: "Can contain less than or equal to 65535 characters").wait_until_present(5)      
    # end
  end 
  context "when submit  degrees" do
    it "then it can contain les than or equal to 255 characters"  do            
      browser.refresh
      text_field(name: "degrees", id: "degrees").clear
      text_field(name: "degrees", id: "degrees").set str(257)
      button(text: "Submit").click       
      span(id: "degrees.errors", text: "Can only be less or equal to 255 characters").wait_until_present(5)            
    end
  end
end

#**************************************************************************
shared_context "custom fields" do
  context "when submit custom text fields" do
    it 'then all required text fields should not be empty' do
      browser.refresh     
      userCustomLab=[]
      labels.each do |lab|    
        if   lab.attribute_value('for') != nil and lab.attribute_value('for').include? "userCustomField" then
          userCustomLab << lab.attribute_value('for')          
        end
      end    
      userCustomLab.each  do |elementName|                                   
        if  label(for: elementName).span(text: "*").exists? then        
          elem = label(for: elementName).element(id: elementName)
          if elem.tag_name == "textarea" or elem.attribute_value('type') == "text" then
            text_field(id: elementName).clear
            button(text: "Submit").click
            errorName = elementName + ".errors"   
            span(id: errorName, text: "Please enter info here.").wait_until_present(8)            
          end         
        end
      end    
    end
  end
  
 context "when submit required select_list" do   
    it 'then required select_list can not be empty' do
      browser.refresh  
      userCustomLab=[]
      labels.each do |lab|    
        if   lab.attribute_value('for') != nil and lab.attribute_value('for').include? "userCustomField" then
          userCustomLab << lab.attribute_value('for')          
        end
      end     
      userCustomLab.each do |elementName|
        STDERR.puts "***"
        STDERR.puts elementName            
        if label(for: elementName).span(text: "*").exists? then                     
          STDERR.puts element(id: elementName).tag_name          
          if element(id: elementName).tag_name == "select" then 
            STDERR.puts (select_list(id: elementName).options).size.to_s
            ((select_list(id: elementName).options).size > 0).should be_truthy               
            STDERR.puts "--- "
          end    
        end
      end  
    end
    it 'then required select_list should be selected' do
      browser.refresh      
      userCustomLab=[]
      labels.each do |lab|    
        if lab.attribute_value('for') != nil and lab.attribute_value('for').include? "userCustomField" then
          userCustomLab << lab.attribute_value('for')          
        end
      end     
      userCustomLab.each do |elementName|
        STDERR.puts "*** ***"
        STDERR.puts elementName            
        if label(for: elementName).span(text: "*").exists? then                     
          STDERR.puts element(id: elementName).tag_name          
          if element(id: elementName).tag_name == "select" then 
            (select_list(id: elementName).selected_options.empty?).should be_falsey
            STDERR.puts "--- ---"
          end
        end
      end                       
    end
  end
end